package cn.elves.model.rule.service.engine.impl;

import cn.elves.model.rule.entity.dto.RuleTreeDTO;
import cn.elves.model.rule.entity.dto.RuleTreeNodeDTO;
import cn.elves.model.rule.service.RuleTreeService;
import cn.elves.model.rule.service.engine.BaseEngine;
import cn.elves.model.rule.service.engine.DecisionResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @description: 规则引擎处理器
 */
@Service("ruleEngineHandle")
public class RuleEngine extends BaseEngine {

    @Resource
    private RuleTreeService ruleTreeService;


    @Override
    public DecisionResult process(Long treeId, Map<String, Object> decisionMatter) {

        RuleTreeDTO ruleTreeDTO = ruleTreeService.queryRuleTree(treeId);
        if (ruleTreeDTO == null) {
            throw new RuntimeException("Rule Tree is null!");
        }

        // 决策节点
        RuleTreeNodeDTO ruleTreeNodeDTO = engineDecisionMaker(ruleTreeDTO, decisionMatter);

        // 决策结果
        return new DecisionResult(true, ruleTreeNodeDTO.getTreeId(), ruleTreeNodeDTO.getId(), ruleTreeNodeDTO.getNodeValue());


    }
}
